package com.jixiang.tracedata.service;

import com.jixiang.tracedata.mapper.TraceOriginalMapper;
import com.jixiang.tracedata.model.Error.OperationErr;
import com.jixiang.tracedata.model.flight.Msg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author quanbiao
 * @date 2018-09-14
 * 航信数据总的实现类
 */
@Service("traceService")
public class TraceService {

    private Logger log = LoggerFactory.getLogger("TraceService");

    @Autowired
    private FlightService flightService;

    @Autowired
    private TravellerService travellerService;

    @Autowired
    private TraceOriginalMapper traceOriginalMapper;

    /**
     * 插入航班时间
     * @param xmlContent
     * @param msg
     */
    @Async("asyncDataBaseTask")
    public void addFlightTraceMsg(String xmlContent, Msg msg){
        try {
            flightService.addTraceFlight(xmlContent, msg);
        } catch (Exception e){
            OperationErr operationErr = new OperationErr();
            operationErr.setErrorXml(xmlContent);
            operationErr.setProcess("flight databaseException");
            traceOriginalMapper.addErrorOperation(operationErr);
            log.error(e.toString(), e);
        }

    }

    /***
     * 插入旅客事件
     * @param xmlContent
     * @param msg
     */
    @Async("asyncDataBaseTask")
    public void addTravellerTraceMsg(String xmlContent, com.jixiang.tracedata.model.traveller.Msg msg){
        try {
            travellerService.addTraceTraveller(xmlContent, msg);
        } catch (Exception e){
            OperationErr operationErr = new OperationErr();
            operationErr.setErrorXml(xmlContent);
            operationErr.setProcess("traveller databaseException");
            traceOriginalMapper.addErrorOperation(operationErr);
            log.error(e.toString(), e);
        }

    }

    /**
     * 操作出现错误入库
     * @param operationErr
     */
    @Async("asyncDataBaseTask")
    public void addOperateErr(OperationErr operationErr){
        try{
            traceOriginalMapper.addErrorOperation(operationErr);
        }catch (Exception e){
            log.error(e.toString(), e);
        }
    }
}
